package com.cyc.basic.leecode.查找;

/**
 * @Description:
 * @version 1.0
 * @author cyc
 * @date 2021/10/7 16:45
 */
public class SearchInsert {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int targetIndex = nums.length;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) {
                left = mid + 1;
            } else if (nums[mid] >= target) {
                targetIndex = mid;
                right = mid - 1;
            }
        }
        return targetIndex;
    }

    public static void main(String[] args) {
        SearchInsert searchInsert = new SearchInsert();
        int[] nums = new int[]{1, 3, 5, 6, 8};
        int target = 7;
        int i = searchInsert.searchInsert(nums, target);
        System.out.println(i);
    }
}
